package org.autumn.current;


import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class ForkJoinTest {
    public static void main(String[] args) {
        // 分治思想
        ForkJoinPool forkJoinPool = new ForkJoinPool(4);
        long start = System.currentTimeMillis();
        System.out.println(forkJoinPool.invoke(new MyTask(100)));
        System.out.println(recursive(100));

        System.out.println(System.currentTimeMillis() - start);
    }

    public static Integer recursive(Integer num) {
        return num == 1 ? 1 : num + recursive(num - 1);
    }
}


class MyTask extends RecursiveTask<Integer> {

    int n;

    public MyTask(int n) {
        this.n = n;
    }

    @Override
    protected Integer compute() {
        if (n == 1) return 1;
        MyTask myTask = new MyTask(n - 1);
        myTask.fork();
        Integer res = myTask.join();
        return n + res;
    }
}